<?php

namespace Admin\Models\read;

use Helpers\Cache;
use Phalcon\Db\Column;
use Phalcon\Di;

class Channel extends BaseRead
{

    public $id;
    public $channelname;        //栏目名称
    public $publishnum;         //发布文章数量
    public $frequency;          //发布频次
    public $publishtime;        //发布时间
    public $parentid;
    public $sortnumber;         //排序编号
    public $type;               //标识（1：栏目；2：标签）
    public $status;             //删除标记（1：正常；2：删除）
    public $learnstages;        //所属学段(存储格式:1,2,3;说明:1：小学；2：初中；3：高中)
    public $iscomment;          //是否可评论 1:可评论 2：不可评论
    public $createuserid;
    public $createtime;
    public $updateuserid;
    public $updatetime;
    public $extra1;
    public $extra2;
    public $extra3;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setConnectionService(parent::Service);
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return parent::getTableName('channel');
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return Channel[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return channel
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 创建model
     *
     * @param $data
     * @return Channel
     *
     * create by ck 20170228
     */
    public static function createRecord($data)
    {
        $obj = !empty($data['id']) ? self::findFirst($data['id']) : new self;
        unset($data['id']);
        $obj or $obj = new self;
        $obj->parentid or $obj->parentid = 0;
        $obj->assign($data);
        return $obj;
    }

    /**
     * 批量删除并返回结果
     *
     * @param $ids
     * @return mixed
     *
     * create by ck 20170228
     * modify by ck 20170318
     */
    public static function deleteRecordById($ids)
    {
        if(!$ids) return true;
        return self::find([
            'conditions' => 'status = 1 AND id IN ({idArr:array})',
            'bind'       => ['idArr' => explode(',', $ids)],
            'bindType'   => ['idArr' => Column::BIND_PARAM_INT]
        ])->delete();
    }

    /**
     * 清除缓存
     *
     * create by ck 20170228
     */
    public static function delCache()
    {
        Cache::delete('read_columns');
        Cache::delete('read_marker');
    }

    /**
     * 获取已定义栏目数组
     *
     * @return Channel[]|array
     *
     * create by ck 20170228
     */
    public static function getColumnsArray()
    {
        $cacheName = 'read_columns';
        if(Cache::exists($cacheName)){
            return Cache::get($cacheName);
        }
        $columns = self::find([
            'columns'       => 'id,channelname',
            'conditions'    => 'status = 1 AND type = 1',
            'order'         => 'sortnumber desc'
        ]);
        if(!$columns){
            Cache::save($cacheName, []);
            return [];
        }
        $columns = $columns->toArray();
        $columns = array_column($columns, 'channelname', 'id');
        Cache::save($cacheName, $columns);
        return $columns;
    }

    /**
     * 获取已定义标签数组
     *
     * @return Channel[]|array
     *
     * create by ck 20170228
     */
    public static function getMarkerArray()
    {
        $cacheName = 'read_marker';
        if (Cache::exists($cacheName)) {
            return Cache::get($cacheName);
        }
        $marker = self::find([
            'columns'       => 'id,channelname',
            'conditions'    => 'status = 1 AND type = 2',
            'order'         => 'sortnumber desc'
        ]);
        if(!$marker){
            Cache::save($cacheName, []);
            return [];
        }
        $marker = $marker->toArray();
        $marker = array_column($marker, 'channelname', 'id');
        Cache::save($cacheName, $marker);
        return $marker;
    }

}
